package com.um.jdy.platform.station.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.device.entity.po.*;
import com.um.jdy.common.device.entity.vo.*;
import com.um.jdy.common.device.service.*;
import com.um.jdy.platform.station.entity.dto.FeeBsbDTO;
import com.um.jdy.platform.station.entity.vo.FeeBsbVO;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class FeeBsbManager extends BaseManager<FeeBsbRentService> {

    @Autowired
    private FeeService feeService;

    @Autowired
    private FeeBsbSwapService feeBsbSwapService;

    public FeeBsbVO get(String id) {
        Fee fee = feeService.getEntity("device_fee_id", id);

        List<FeeBsbRent> rentList = service.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"price"}, new String[]{"asc"});
        List<FeeBsbRentVO> rents = UMBeanUtils.copyBeanList(rentList,FeeBsbRentVO.class);

        List<FeeBsbSwap> swapList = feeBsbSwapService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"price"}, new String[]{"asc"});
        List<FeeBsbSwapVO> swaps = UMBeanUtils.copyBeanList(swapList,FeeBsbSwapVO.class);

        FeeBsbVO feeBsbVO = new FeeBsbVO();
        feeBsbVO.setName(fee.getName());
        feeBsbVO.setRemark(fee.getRemark());
        feeBsbVO.setPlatformMerchantId(fee.getPlatformMerchantId());
        feeBsbVO.setRents(rents);
        feeBsbVO.setSwaps(swaps);
        return feeBsbVO;
    }

    @Transactional
    public Response add(FeeBsbDTO feeBsbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeBsbDTO, Fee.class);
        fee.setCreateTime(new Date());
        fee.setDeviceType(DeviceTypeEnum.BSB.getKey());

        feeService.addEntity(fee);

        //保存租电套餐
        feeBsbDTO.getRents().stream().forEach(feeBsbRent -> {
            feeBsbRent.setDeviceFeeId(fee.getDeviceFeeId());
            service.add(BeanUtil.toBean(feeBsbRent, FeeBsbRent.class));
        });

        //保存换电套餐列表
        feeBsbDTO.getSwaps().stream().forEach(feeBsbSwap -> {
            feeBsbSwap.setDeviceFeeId(fee.getDeviceFeeId());
            feeBsbSwapService.add(BeanUtil.toBean(feeBsbSwap, FeeBsbSwap.class));
        });

        return Response.success();
    }

    @Transactional
    public Response update(String id, FeeBsbDTO feeBsbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeBsbDTO,Fee.class);
        fee.setDeviceFeeId(id);
        fee.setUpdateTime(new Date());
        feeService.updateEntity(fee);

        //删除旧租电套餐
        service.delete("device_fee_id", id);
        //保存新租电套餐
        feeBsbDTO.getRents().stream().forEach(feeBsbRent -> {
            feeBsbRent.setDeviceFeeId(fee.getDeviceFeeId());
            service.add(BeanUtil.toBean(feeBsbRent, FeeBsbRent.class));
        });

        //删除旧换电套餐列表
        feeBsbSwapService.delete("device_fee_id", id);
        //保存新换电套餐列表
        feeBsbDTO.getSwaps().stream().forEach(feeBsbSwap -> {
            feeBsbSwap.setDeviceFeeId(fee.getDeviceFeeId());
            feeBsbSwapService.add(BeanUtil.toBean(feeBsbSwap, FeeBsbSwap.class));
        });

        return Response.success();
    }
}
